package com.wgke.bluetooth.utils;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Environment;
import android.util.Log;

import androidx.core.content.ContextCompat;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.wgke.bluetooth.db.CmdEntity;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.lang.reflect.Type;
import java.util.List;
import java.util.Objects;

public class FileUtil {

    public static List<CmdEntity> readJsonFile(Uri uri, Context c) {
        try {
            // 使用 ContentResolver 读取文件内容
            InputStream inputStream = c.getContentResolver().openInputStream(uri);
            BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
            StringBuilder stringBuilder = new StringBuilder();
            String line;
            while ((line = reader.readLine()) != null) {
                stringBuilder.append(line);
            }
            inputStream.close();

            // 获取完整的 JSON 字符串
            String jsonString = stringBuilder.toString();
            Log.d("JSON", "File content: " + jsonString);

            // 解析 JSON（可选）
            try {
                Gson gson = new Gson();
                Type userListType = new TypeToken<List<CmdEntity>>() {
                }.getType();
                List<CmdEntity> data = gson.fromJson(jsonString, userListType);
                return data;
            } catch (Exception e) {
                e.printStackTrace();
            }

        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static String loadJsFromAssets(String fileName) {
        try {
            InputStream is = CommonUtil.getContext().getAssets().open(fileName);
            BufferedReader reader = new BufferedReader(new InputStreamReader(is));
            StringBuilder sb = new StringBuilder();
            String line;
            while ((line = reader.readLine()) != null) {
                sb.append(line).append("\n");
            }
            reader.close();
            return sb.toString();
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }

    // 将drawable资源转换为PNG并保存到外部存储
    public static boolean convertDrawableToPng(Context context, int drawableResId, String fileName) {
        // 获取drawable
        Drawable drawable = ContextCompat.getDrawable(context, drawableResId);
        if (drawable == null) {
            return false;
        }

        // 创建一个与drawable相同大小的位图
        Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);

        // 创建画布并绘制drawable
        Canvas canvas = new Canvas(bitmap);
        drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
        drawable.draw(canvas);

        // 保存位图为PNG文件
        File file = new File(context.getExternalFilesDir(Objects.requireNonNull(context.getExternalFilesDir("res")).getAbsolutePath()), fileName);
        try (FileOutputStream out = new FileOutputStream(file)) {
            bitmap.compress(Bitmap.CompressFormat.PNG, 100, out); // 100表示最高质量
            return true;
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        } finally {
            bitmap.recycle();
        }
    }
}
